package org.sunrise.game.center.server;

import org.sunrise.game.common.network.server.BaseServer;
import org.sunrise.game.common.network.server.BaseServerManager;
import org.sunrise.game.common.utils.IdGenerator;
import org.sunrise.game.common.utils.Utils;

public class CenterServer {
    private final String ip;
    private final int port;
    private final BaseServer server;

    public CenterServer(int id, String ip, int port) {
        this.ip = ip;
        this.port = port;
        this.server = new BaseServer(this.getClass().getSimpleName() + id) {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onStop() {
                if (isStartSuccess()) {
                    super.onStop();
                }
            }
        };
        this.server.setMessageManager(new CenterServerMessageManager(server.getNodeId()));
        BaseServerManager.register(server);
        IdGenerator.init(1000 + id);
    }

    public String getNodeId() {
        return server.getNodeId();
    }

    public void start() {
        server.startListen(Utils.getListenIpAddress(), port);
    }

}
